package com.cqie.tes.config;

import com.cqie.tes.comm.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //指定处理什么异常
//    @ExceptionHandler(Exception.class)
//    public R error(Exception e){
//        e.printStackTrace();
//        return R.error().message("执行了全局异常");
//    }

    //自定义异常
    @ExceptionHandler(MyException.class)
    public R error(MyException e){
        log.error(e.getMessage());
        e.printStackTrace();
        return R.error().code(e.getCode()).message(e.getMsg());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public R handleAuthorizationException(AccessDeniedException e)
    {
        log.error(e.getMessage());
        return R.error().message("没有权限");
    }
}